from aiogram import F
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.kbd import Back, Button, Cancel, ScrollingGroup, Select
from aiogram_dialog.widgets.text import Format

from src.presentation.tg_bot.dialogs.getters.proxies import order_proxies_getter, proxy_getter
from src.presentation.tg_bot.dialogs.widgets import LocaleText
from src.presentation.tg_bot.states import ProxyDetailsSG

from .handlers import check_proxy_speed, select_proxy_id

order_proxies_window = Window(
    LocaleText("select-proxy"),
    ScrollingGroup(
        Select(
            Format("{item.login}:{item.password}:{item.port_https}"),
            "catselsel",
            lambda proxy: proxy.id,
            "proxies",
            on_click=select_proxy_id,
        ),
        width=2,
        height=4,
        id="catsel",
        hide_on_single_page=True,
    ),
    Cancel(LocaleText("back-btn")),
    state=ProxyDetailsSG.select_proxy,
    getter=order_proxies_getter,
)

proxy_details_window = Window(
    LocaleText("proxy-details"),
    LocaleText("proxy_id", proxy_id="{proxy_id}"),
    LocaleText("proxy-type", proxy_type="{proxy_type_translated}"),
    LocaleText("ip-address", ip_address="{ip_address}"),
    LocaleText(
        "proxy-login",
        login="{login}",
        when=(F["proxy_privacy"] == "private") & (F["authorization_type"] == "by_password")
        | (F["proxy_type"] == "server"),
    ),
    LocaleText(
        "proxy-password",
        password="{password}",
        when=(F["proxy_privacy"] == "private") & (F["authorization_type"] == "by_password")
        | (F["proxy_type"] == "server"),
    ),
    LocaleText("https-port", port_https="{port_https}"),
    LocaleText("proxy-country", country="{country}"),
    LocaleText("proxy-category", category="{category}", when=F["proxy_privacy"] == "shared"),
    LocaleText("proxy-provider", provider="{provider}"),
    LocaleText("rotation-type", rotation_type="{rotation_type}", when=F["rotation_type"]),
    LocaleText(
        "rotation-period",
        rotation_period="{rotation_period}",
        when=F["rotation_period"],
    ),
    LocaleText(
        "authorization-type",
        authorization_type="{authorization_type_translated}",
        when=F["authorization_type_translated"],
    ),
    LocaleText(
        "authorization-ip",
        authorization_ip="{authorization_ip}",
        when=F["authorization_ip"],
    ),
    LocaleText(
        "reload_modem_link",
        reload_modem_link="{reload_modem_link}",
        when=F["reload_modem_link"],
    ),
    LocaleText("proxy-site", site="{site}", when=F["proxy_privacy"] == "shared"),
    Button(
        LocaleText("check-connection-speed-btn"),
        id="check_speed",
        on_click=check_proxy_speed,
    ),
    Back(LocaleText("back-btn")),
    state=ProxyDetailsSG.proxy_details,
    getter=proxy_getter,
)

proxy_details_dialog = Dialog(
    order_proxies_window,
    proxy_details_window,
)
